home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: Dan Pop <danpop@mail.cern.ch>
- Newsgroups: comp.lang.c.moderated,comp.std.c
- Subject: Re: Function returning structure
- Date: 24 Jan 1996 11:08:33 -0600
- Organization: CERN European Lab for Particle Physics
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4e5p2h$6hk@solutions.solon.com>
- References: <4e2ki8$l0k@solutions.solon.com>
- NNTP-Posting-Host: solutions.solon.com
- X-NNTP-Posting-Host: hpl3sn03.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
- X-Mail2News-Path: disperse.demon.co.uk!post.demon.co.uk!dxmint.cern.ch!hpl3sn03.cern.ch
-
- bnelson@netcom.com (Bob Nelson) writes:
-
- >/*
- >Consider the following code -- in which a structure member is of type
- >array of char and an instance of that structure is returned from a
- >a function.
- >
- >The Standard [6.3.2.3] says that the dot operator designates a member
- >of a structure. (It goes on to add -- in its example text -- that f().x
- >is a valid postfix expression though not an lvalue, presuming that
- ^^^^^^^^^^^^^^^^^^^^
- This is the clue you were looking for!
-
- >f is a function returning a structure and that x is member).
-
- >With that in mind -- does the standard have anything that might
- >disqualify member selection (in a non-assignment context) if that
- >member is of type array and that structure is returned by a function?
- >One current, popular compiler fails to compile this code -- diagnosing
- >an "invalid use of non-lvalue array".
-
- gcc was right, IMHO.
-
- >*/
- >
- >#include <stdio.h>
- >
- >struct T {
- > char c[81];
- >};
- >
- >struct T func(void)
- >{
- > static struct T s = { "returned from func" };
- >
- > return s;
- >}
- >
- >int main(void)
- >{
- >
- > printf("%s\n", func().c);
- > return 0;
- >}
-
- The compiler has to take the address of func().c and pass it to printf.
- But func().c is not an lvalue, hence its address cannot be taken.
- I'm not sure this is the only possible interpretation, so I'm crossposting
- this article to comp.std.c.
-
- The OSF/1 and Solaris compilers complain too, but the most "interesting"
- behaviour is displayed by the IRIX compiler:
-
- sgiref:~/tmp 7> uname -a
- IRIX sgiref 5.2 02282016 IP22 mips
- sgiref:~/tmp 8> cc test.c
- sgiref:~/tmp 9> cc -ansi test.c
- Assertion failed: !IS_STRICT_ANSI, file expr_cg.c, line 2285
- cfe: Fatal: test.c: IOT instruction
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-